package com.tledu.mr;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Student implements Writable {
    private String id;
    private String name;
    private String courseNo;
    private String courseName;
    private String province;
    private String city;
    private String area;


    public Student() {
    }

    public Student(String id, String name, String courseNo, String courseName) {
        this.id = id;
        this.name = name;
        this.courseNo = courseNo;
        this.courseName = courseName;
        this.province = "";
        this.city = "";
        this.area = "";
    }

    public static Student newStudentLocation(String id, String province, String city, String area) {
        Student stu = new Student();
        stu.setId(id);
        stu.setProvince(province);
        stu.setCity(city);
        stu.setArea(area);
        stu.setName("");
        stu.setCourseNo("");
        stu.setCourseName("");
        return stu;
    }

    // 将from copy到to里面
    public static Student copy(Student to,Student from) {
        if (from.getName() != null && from.getName().length() > 0) {
            // 学生信息
            to.setId(from.getId());
            to.setName(from.getName());
            to.setCourseNo(from.getCourseNo());
            to.setCourseName(from.getCourseName());
        }
        if (from.getProvince() != null && from.getProvince().length() > 0) {
            to.setId(from.getId());
            to.setProvince(from.getProvince());
            to.setCity(from.getCity());
            to.setArea(from.getArea());
        }
        return to;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", courseNo='" + courseNo + '\'' +
                ", courseName='" + courseName + '\'' +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", area='" + area + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCourseNo() {
        return courseNo;
    }

    public void setCourseNo(String courseNo) {
        this.courseNo = courseNo;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(id);
        if (name != null) {
            dataOutput.writeUTF(name);
        }
        if (courseNo != null) {
            dataOutput.writeUTF(courseNo);
        }
        if (courseName != null) {
            dataOutput.writeUTF(courseName);
        }
        if (province != null) {
            dataOutput.writeUTF(province);
        }
        if (city != null) {
            dataOutput.writeUTF(city);
        }
        if (area != null) {
            dataOutput.writeUTF(area);
        }
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.id = dataInput.readUTF();
        this.name = dataInput.readUTF();
        this.courseNo = dataInput.readUTF();
        this.courseName = dataInput.readUTF();
        this.province = dataInput.readUTF();
        this.city = dataInput.readUTF();
        this.area = dataInput.readUTF();
    }
}
